interface ToastOptions extends UniApp.ShowToastOptions {
  custom?: boolean
  message?: string
  msg?: string
  errMsg?: string
  duration?: number
  callback?: () => void
}

type FtToastOptions = string | ToastOptions
type FtShowLoadingOptions = UniApp.ShowLoadingOptions & {
  duration?: number
  callback?: () => void
}

// 定义 show 函数类型
type ShowFunction = (options?: FtToastOptions, callback?: () => void) => void

// 定义 error 函数类型
type ErrorFunction = (options?: ToastOptions | string, callback?: () => void) => void

// 定义 success 函数类型
type SuccessFunction = (options?: ToastOptions) => void

// 定义 showLoading 函数类型
type ShowLoadingFunction = (options?: UniApp.ShowLoadingOptions, callback?: () => void) => void

// 定义 hideLoading 函数类型
type HideLoadingFunction = (callback?: () => void, duration?: number) => void

// 定义扩展后的 show 对象类型
interface ExtendedShowFunction extends ShowFunction {
  show: ShowFunction
  success: SuccessFunction
  error: ErrorFunction
  showLoading: ShowLoadingFunction
  hideLoading: HideLoadingFunction
}

/**
 * 显示Toast
 * @param options
 * @param callback
 */
const show: ExtendedShowFunction = (options: FtToastOptions = {}, callback?: () => void): void => {
  if (typeof options === 'string') {
    const duration = 2000
    uni.showToast({
      icon: 'none',
      duration,
      title: options,
    })
    setTimeout(() => {
      callback && callback()
    }, duration + 50)
  } else {
    const isResultObj = Object.keys(options).filter(item => item === 'custom').length === 1
    if (isResultObj) {
      error(options, callback)
    } else {
      const title = typeof options === 'string' ? options : options.message || options.errMsg || options.msg || '未知'
      const duration = options.duration || 2000
      uni.showToast({
        icon: 'none',
        duration,
        ...options,
        title,
      })
      setTimeout(() => {
        if (callback) {
          callback()
        } else if (options.callback) {
          options.callback()
        }
      }, duration + 50)
    }
  }
}

/**
 * 错误显示
 * @param options
 * @param callback
 */
function error(options: ToastOptions | string = {}, callback?: () => void): void {
  const duration = 2000
  if (typeof options === 'string' && options.includes('500 Server Error')) {
    options = {
      message: '服务器错误，请联系管理员',
      callback,
    }
  }
  const title = typeof options === 'string' ? options : options.message || options.errMsg || options.msg || '未知错误'
  uni.showToast({
    icon: 'none',
    duration,
    title,
  })
  if (callback) {
    if (typeof options === 'object') {
      options.callback = callback
    }
  }
  setTimeout(() => {
    if (typeof options === 'object' && options.callback) {
      options.callback()
    }
  }, duration)
}

/**
 * 显示成功
 * @param options
 */
function success(options: ToastOptions = {}): void {
  const duration = options.duration || 1500
  const title = typeof options === 'string' ? options : options.message || options.errMsg || options.msg || '未知'

  uni.showToast({
    icon: 'success',
    duration,
    ...options,
    title,
  })
  setTimeout(() => {
    options.callback && options.callback()
  }, duration)
}

/**
 * 显示加载中
 * @param options
 * @param callback
 */
function showLoading(options: FtShowLoadingOptions = {}, callback?: () => void): void {
  uni.showLoading({
    mask: false,
    title: '请稍候...',
    ...options,
  })
  options.callback && callback()
  if (options.duration && options.duration > 0) {
    hideLoading(undefined, options.duration)
  }
}

/**
 * 隐藏Loading
 * @param callback
 * @param duration
 */
function hideLoading(callback?: () => void, duration: number = 10): void {
  setTimeout(() => {
    uni.hideLoading()
    callback && callback()
  }, duration)
}

show.show = show
show.success = success
show.error = error
show.showLoading = showLoading
show.hideLoading = hideLoading

// 假设挂载到 window 对象上

export default show

export {
  error,
  hideLoading,
  show,
  showLoading,
  success,
}
